var express = require('express');
var router = express.Router();
var { searchModel, adsModel, typeModel, shopModel } = require('../../../model/model');
// 使用multiparty对获取前端上传的图片或者文件
var multiparty = require('multiparty')
// 导入node-xlsx对上传过来的文件中的内容进行读取
var xlsx = require('node-xlsx')

// 获取搜索热词
router.get('/getsearch', async (req, res) => {
  let user = req.query.data
  if (user != undefined) {
    if (user.item != '') {
      let data = await searchModel.find({ name: RegExp(user.item) });
      res.send({
        data: data
      })
    } else {
      let data = await searchModel.find();
      res.send({
        code: 200,
        data: data
      });
    }
  } else {
    let data = await searchModel.find();
    res.send({
      code: 200,
      data: data
    });
  }

})

// 添加热词
router.post('/addsearch', async (req, res) => {
  let from = req.body.data.from
  console.log(from);
  await searchModel.create(from);
  res.send({
    code: 200,
  });
})

// 修改热词状态
router.post('/updatestatus', async (req, res) => {
  let data = req.body.data
  if (data.type == '上线') {
    await searchModel.updateOne({ _id: data._id }, { state: false })
  } else {
    await searchModel.updateOne({ _id: data._id }, { state: true })
  }
  res.send({
    code: 200,
  })
})

// 删除热词
router.post('/deleteworks', async (req, res) => {
  let data = req.body.data
  await searchModel.deleteOne({ _id: data._id })
  res.send({
    code: 200
  })
})

// 修改热词
router.post('/updateworksinfo', async (req, res) => {
  let data = req.body.data.from
  console.log();
  if (data.state == '上线') {
    data.state = true
    await searchModel.updateOne({ _id: data._id }, data)
  } else {
    data.state = false
    await searchModel.updateOne({ _id: data._id }, data)
  }
  res.send({
    code: 200,
  })
})



//广告管理
// 图片上传
router.post('/imgupload', async (req, res) => {
  let form = new multiparty.Form()
  form.uploadDir = 'upload'
  form.parse(req, (err, a, b) => {
    res.send({
      code: 200,
      img: "http://localhost:3000/" + b.avatar[0].path,
    })
  })
})

// 添加广告
router.post('/addads', async (req, res) => {
  let data = req.body.data.from
  await adsModel.create(data)
  res.send({
    code: 200,
    data
  })
})

// 获取广告列表
router.get('/getads', async (req, res) => {
  let item = req.query.data
  console.log(item);
  if (item != undefined) {
    if (item.item != '') {
      let data = await adsModel.find({ name: RegExp(item.item) })
      res.send({
        code: 200,
        data
      })
    } else {
      let data = await adsModel.find()
      res.send({
        code: 200,
        data
      })
    }
  } else {
    let data = await adsModel.find()
    res.send({
      code: 200,
      data
    })
  }
})

// 修改广告状态
router.post('/updateads', async (req, res) => {
  let data = req.body.data
  if (data.state == '上线') {
    console.log(1111);
    await adsModel.updateOne({ _id: data._id }, { $set: { state: false } })
  } else {
    data.state = true
    await adsModel.updateOne({ _id: data._id }, { $set: { state: true } })
  }
  res.send({
    code: 200,
  })
})

// 删除广告
router.post('/deleteads', async (req, res) => {
  let data = req.body.data
  await adsModel.deleteOne({ _id: data._id })
  res.send({
    code: 200
  })
})

// 修改热词
router.post('/updateadsinfo', async (req, res) => {
  let data = req.body.data.from
  console.log(data);
  if (data.state == '上线') {
    data.state = true
    await adsModel.updateOne({ _id: data._id }, data)
  } else {
    data.state = false
    await adsModel.updateOne({ _id: data._id }, data)
  }
  res.send({
    code: 200,
  })
})

// 专题
router.get('/gettopic', async (req, res) => {
  let item = req.query.data
  if (item != undefined) {
    if (item.item != '') {
      let data = await shopModel.find()
      let type = await typeModel.find({ type: RegExp(item.item) }).lean()
      for (let i in type) {
        for (let j in data) {
          if (String(type[i]._id) == String(data[j].type)) {
            if (!type[i].num) {
              type[i].num = 1
            } else {
              type[i].num += 1
            }
          }
        }
      }
      res.send({
        code: 200,
        data: type
      })
    } else {
      let data = await shopModel.find()
      let type = await typeModel.find().lean()
      for (let i in type) {
        for (let j in data) {
          if (String(type[i]._id) == String(data[j].type)) {
            if (!type[i].num) {
              type[i].num = 1
            } else {
              type[i].num += 1
            }
          }
        }
      }
      res.send({
        code: 200,
        data: type
      })
    }
  } else {
    let data = await shopModel.find()
    let type = await typeModel.find().lean()
    for (let i in type) {
      for (let j in data) {
        if (String(type[i]._id) == String(data[j].type)) {
          if (!type[i].num) {
            type[i].num = 1
          } else {
            type[i].num += 1
          }
        }
      }
    }
    res.send({
      code: 200,
      data: type
    })
  }
})

// 修改专题状态
router.post('/updatetopic', async (req, res) => {
  let data = req.body.data
  console.log(data);
  if (data.state == '禁用') {
    await typeModel.updateOne({ _id: data._id }, { item_type: true })
  } else {
    await typeModel.updateOne({ _id: data._id }, { item_type: false })
  }
  res.send({
    code: 200,
  })
})

// 删除专题
router.post('/deletetopic', async (req, res) => {
  let data = req.body.data
  await typeModel.deleteOne({ _id: data._id })
  res.send({
    code: 200
  })
})


module.exports = router;
